﻿using System;
using System.IO;
using System.Net;
using HtmlAgilityPack;

namespace DullBoard.Data
{
    internal class WebResourceFetcher
    {
        private string _data;


        public HtmlDocument Fetch(Uri uri)
        {    
            var webClient = new DullWebRequest();
            webClient.TimeOut = 30;
            _data = webClient.DownloadString(uri);
             
            var fs = new FileStream(@"c:\temp\" + Guid.NewGuid().ToString() + ".txt", FileMode.Create, FileAccess.Write);
            var tw = new StreamWriter(fs);

            tw.Write(_data);
            tw.Flush();
            tw.Close();
            
            var doc = new HtmlDocument();
            doc.LoadHtml(_data);
            return doc;
        }
    }

    public class DullWebRequest : WebClient
    {
        public int TimeOut { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            request.Timeout = TimeOut * 1000;
            return request;
        }
    }
}
